//
//  GYForgetViewController.m
//  research
//
//  Created by 章鱼 on 17/4/5.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYForgetViewController.h"
#import "GYRegisterTextfieldView.h"
#import "GYLoginViewController.h"

@interface GYForgetViewController ()

@property (nonatomic, strong) GYRegisterTextfieldView *phoneNumberView;//手机
@property (nonatomic, strong) GYRegisterTextfieldView *emailNumberView;//邮箱
@property (nonatomic, strong) GYRegisterTextfieldView *passwordNumberView;//密码
@property (nonatomic, strong) GYRegisterTextfieldView *surepasswordNumberView;//确认密码
@property (nonatomic, strong) UIButton *slectedButton;
@end

@implementation GYForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetup];
    [self initForgetView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addBackButtonItemWithTitle:@"返回" backImg:@"navigation-bar_back_normal@2x"];
}


- (void)basicSetup {
    [self initNavBarWithTitle:@"忘记密码" tintColor:[UIColor orz_mainColor]];
    [self.navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor],NSForegroundColorAttributeName,
                                         [UIFont boldSystemFontOfSize:18],
                                         NSFontAttributeName,
                                         nil]];
    [self.view setBackgroundColor:[UIColor orz_globalBackground]];
    
}

- (void)initForgetView {

    _phoneNumberView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_phoneNumberView];
    _phoneNumberView.titleLable.text = @"手机";
    
    _phoneNumberView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请您输入常用手机号码" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _emailNumberView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_emailNumberView];
    _emailNumberView.titleLable.text = @"邮箱";
    _emailNumberView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请您输入常用电子邮箱" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _passwordNumberView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_passwordNumberView];
    _passwordNumberView.titleLable.text = @"密码";
    _passwordNumberView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请您输入2~9的密码" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _surepasswordNumberView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_surepasswordNumberView];
    _surepasswordNumberView.titleLable.text = @"确认密码";
    _surepasswordNumberView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"为了保险请再来一遍" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _slectedButton = [[UIButton alloc] init];
    
    [self.view addSubview:_slectedButton];
    [_slectedButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _slectedButton.backgroundColor = [UIColor orz_mainColor];
    [_slectedButton setTitle:@"完成" forState:UIControlStateNormal];
    _slectedButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _slectedButton.layer.cornerRadius = 4.f;
    _slectedButton.layer.masksToBounds = YES;
    [_slectedButton addTarget:self action:@selector(selectedClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self initRegisterViewUI];
}

- (void)initRegisterViewUI {
    
    [_phoneNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(84.f);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_emailNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_phoneNumberView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_passwordNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_emailNumberView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_surepasswordNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_passwordNumberView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_slectedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_surepasswordNumberView.mas_bottom).offset(20.f);
        make.left.mas_equalTo(30.f);
        make.right.mas_equalTo(-30.f);
        make.height.mas_equalTo(44.f);
    }];
    
}

- (void)selectedClick:(UIButton *)btn {
    [[YGHttpManager defaultManager] fetchGetPassWordWithWithphone:_phoneNumberView.textFiled.text email:_emailNumberView.textFiled.text newPasswd:_passwordNumberView.textFiled.text success:^(id responseObj) {
        GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
        if (commonmodel.status == 0) {
            [VBAlertView showTextHUD:commonmodel.message];
        }
        if (commonmodel.status == 1) {
            
            GYUserInfoModel *userModel = [GYUserInfoModel mj_objectWithKeyValues:commonmodel.data];
            [[GYConfigDBManager defaultManager] setUserInformation:userModel];
            [[GYConfigDBManager defaultManager] setLogin:YES];
            [VBAlertView showTextHUD:@"修改密码成功"];
            GYLoginViewController *loginCtrl = [[GYLoginViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
            [self presentViewController:nav animated:YES completion:NULL];
            
        }

        
    } failure:^(NSError *error) {
        
    }];
    
}




@end
